package com.wondersgroup.fjzlpt.org.controller.rest.wdqyhz;

import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.quick.framework.boot.remoteResult.ResultObject;
import com.quick.framework.boot.web.wapper.QuickPrincipal;
import com.quick.framework.exception.ServiceException;
import com.quick.framework.http.beans.HttpBean;
import com.quick.framework.http.inter.IHttpClientOperator;
import com.quick.framework.util.LoggerUtil;
import com.quick.framework.util.QuickPrincipalUtils;
import com.quick.framework.util.WsXmlUtil;
import com.wondersgroup.fjzlpt.org.feign.SxzzApiFeignService;
import com.wondersgroup.fjzlpt.org.model.Referral;
import com.wondersgroup.fjzlpt.org.util.ControllerCommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.quick.framework.boot.web.utils.RequestUtils;
import com.wondersgroup.fjzlpt.org.service.inter.wdqyhz.WdqyhzService;

@RestController
@RequestMapping("wdqyhz")
public class WdqyhzRestController {

	@Autowired
	private WdqyhzService wdqyhzService;

	@Autowired
	private SxzzApiFeignService sxzzApiFeignService;

    @Autowired
    private IHttpClientOperator httpClientOperator;
	
	/**
	 * 分页查询已分配任务
	 * @param request
	 * @return
	 */
	@RequestMapping("queryYfprwByPage")
	public JSONObject queryYfprwByPage(HttpServletRequest request) {
		Map<String, String> map = RequestUtils.GetParameterMap(request);
		return wdqyhzService.queryYfprwByPage(map);
	}
	
	/**
	 * 分页查询我的签约患者
	 * @param request
	 * @return
	 */
	@RequestMapping("/queryPage")
	public JSONObject queryPage(HttpServletRequest request) {
		Map<String, String> map = RequestUtils.GetParameterMap(request);
		return wdqyhzService.queryPage(map);
	}
	
	/**
	 * 根据签约信息ids查询所有的服务信息
	 * @param request
	 * @return
	 */
	@RequestMapping("queryFwxxByQyxxids")
	public JSONObject queryFwxxByQyxxids(HttpServletRequest request) {
		Map<String, String> map = RequestUtils.GetParameterMap(request);
		return wdqyhzService.queryFwxxByQyxxids(map);
	}
	
	/**
	 * 保存分配的服务
	 * --新增/修改/完成
	 * @param request
	 * @return
	 */
	@RequestMapping("save")
	public JSONObject save(HttpServletRequest request) {
		Map<String, String> map = RequestUtils.GetParameterMap(request);
		map.put("USERID",QuickPrincipalUtils.getUserID(request)); //实际服务人员ID
		return wdqyhzService.save(map);
	}
	
	/**
	 * 分页查询已经分配的服务项（已完成/待完成）
	 * @param request
	 * @return
	 */
	@RequestMapping("queryYfpfwxByPage")
	public JSONObject queryYfpfwxByPage(HttpServletRequest request) {
		Map<String, String> map = RequestUtils.GetParameterMap(request);
		map.put("login_userId",QuickPrincipalUtils.getUserID(request));
		return wdqyhzService.queryYfpfwxByPage(map);
	}
	
	/**
	 * 查询某服务项已经完成的次数和总次数
	 * @param request
	 * @return
	 */
	@RequestMapping("queryNumberOfYwcrw")
	public JSONObject queryNumberOfYwcrw(HttpServletRequest request) {
		Map<String, String> map = RequestUtils.GetParameterMap(request);
		return wdqyhzService.queryNumberOfYwcrw(map);
	}
	
	/**
	 * 根据团队id查询团队成员
	 * @param request
	 * @return
	 */
	@RequestMapping("queryTdry")
	public JSONArray queryTdry(HttpServletRequest request) {
		Map<String, String> map = RequestUtils.GetParameterMap(request);
		return wdqyhzService.queryTdry(map);
	}
	
	/**
	 * 查询以当前用户作为团队长的团队中相关签约的服务包
	 * @param request
	 * @return
	 */
	@RequestMapping("queryFwbxxByUserInfo")
	public JSONArray queryFwbxxByYljgid(HttpServletRequest request) {
		Map<String, String> map = RequestUtils.GetParameterMap(request);
		return wdqyhzService.queryFwbxxByUserInfo(map);
	}
	
	/**
	 * 删除已分配任务
	 * @param request
	 * @return
	 */
	@RequestMapping("deleteYfprw")
	public JSONObject deleteYfprw(HttpServletRequest request) {
		Map<String, String> map = RequestUtils.GetParameterMap(request);
		return wdqyhzService.deleteYfprw(map);
	}
	
	/**
	 * 查询服务居民的基本信息
	 * @param request
	 * @return
	 */
	@RequestMapping("queryJmxx")
	public JSONObject queryJmxx(HttpServletRequest request) {
		Map<String, String> map = RequestUtils.GetParameterMap(request);
		return wdqyhzService.queryJmxx(map);
	}

	/**
	 * 查询"查看服务"的标签页统计数目
	 * @param request
	 * @return
	 */
	@RequestMapping("queryCountInCkfw")
	public JSONObject queryCountInCkfw(HttpServletRequest request) {
		Map<String, String> map = RequestUtils.GetParameterMap(request);
		return wdqyhzService.queryCountInCkfw(map);
	}
	
	/**
	 * 通过居民信息id查询该患者的家庭成员的签约信息
	 * @param request
	 * @return
	 */
	@RequestMapping("queryJtcyQyxxByJmxxid")
	public JSONObject queryJtcyQyxxByJmxxid(HttpServletRequest request){
		Map<String, String> map = RequestUtils.GetParameterMap(request);
		return wdqyhzService.queryJtcyQyxxByJmxxid(map);
	}
	
	/**
	 * 查询可以分配任务的家庭成员
	 * @param request
	 * @return
	 */
	@RequestMapping("queryDfwjm")
	public JSONObject queryDfwjm(HttpServletRequest request){
		Map<String, String> map = RequestUtils.GetParameterMap(request);
		return wdqyhzService.queryDfwjm(map);
	}
	
	/**
	 * 导出签约患者数据
	 * @param request
	 * @param response
	 */
	@RequestMapping("downloadWdqyhz")
	public void downloadWdqyhz(HttpServletRequest request,HttpServletResponse response){
		Map<String, String> map = RequestUtils.GetParameterMap(request);
		wdqyhzService.downloadWdqyhz(map,request,response);
	}
	
	/**
	 * 导出已分配任务数据（任务管理页签）
	 * @param request
	 * @param response
	 */
	@RequestMapping("downloadYfprw")
	public void downloadYfprw(HttpServletRequest request,HttpServletResponse response){
		Map<String, String> map = RequestUtils.GetParameterMap(request);
		wdqyhzService.downloadYfprw(map,request, response);
	}
	
	/**
	 * 导出服务记录数据（已完成与未完成）
	 * @param request
	 * @param response
	 */
	@RequestMapping("downloadFwjl")
	public void downloadFwjl(HttpServletRequest request,HttpServletResponse response){
		Map<String, String> map = RequestUtils.GetParameterMap(request);
		wdqyhzService.downloadFwjl(map,request,response);
	}

	/**
	 * 跳转双转系统
	 * @param request
	 * @param response
	 */
	@RequestMapping("forwardSzxt")
	public String forwardSzxt(HttpServletRequest request,HttpServletResponse response){
		Map<String, String> map = RequestUtils.GetParameterMap(request);
		QuickPrincipal qp = (QuickPrincipal)request.getUserPrincipal();
		ResultObject ro = new ResultObject();
		JSONObject userInfo = qp.getUserInfo();
		Referral referral = new Referral();
		referral.setOrderid(UUID.randomUUID().toString());
		referral.setFqyydm(userInfo.getString("YLJGDM"));
		//referral.setFqyydm("33060247139160X11C2101");
		referral.setFqyymc(userInfo.getString("YLJGMC"));
		referral.setFqksdm(userInfo.getString("KSDM"));
		referral.setFqksmc(userInfo.getString("KSMC"));
		referral.setFqysgh(userInfo.getString("YSGH"));
		//referral.setFqysgh("22");
		referral.setFqysxm(userInfo.getString("XM"));
		referral.setHzxm(map.get("XM"));
		referral.setJzkh(map.get("ZJHM"));
		referral.setJzklx("8");
		referral.setSfzh(map.get("ZJHM"));
		referral.setCsrq(map.get("CSRQ"));
		referral.setLxdh(map.get("SJHM"));
		referral.setJtdz(map.get("JTZZ"));
		referral.setMzzycxbz(map.get("TYPE"));
		try {
			String xmlStr = WsXmlUtil.BeanToXml(referral,Referral.class,"UTF-8",false,true);
			//sxzzApiFeignService.reservation(xmlStr);
            HttpBean hb = new HttpBean(ControllerCommonUtil.getApiUrl(), xmlStr);
            String result = httpClientOperator.postJson(hb);
            //String result = sxzzApiFeignService.reservation(xmlStr);
            //System.out.println(result);
			if(result.indexOf("error=") != -1){
                result = result.substring( 6 , result.length());
                ro.changeFaultState(result);
            }else{
                ro.setInfo("URL" , ControllerCommonUtil.getSxzzUrl() + result);
            }
		} catch (Exception e) {
			LoggerUtil.error(e.getMessage());
			throw new ServiceException("ERR-000807","转诊出错了，" + e.getMessage());
		}
		return ro.toJsonStr() ;
	}
	
}
